package com.leyou.item.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.BrandMapper;


import com.leyou.item.service.BrandService;
import com.leyou.item.service.CategoryBrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.print.Printable;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper,Brand> implements BrandService {
    @Autowired
    private CategoryBrandService categoryBrandService;

    @Override
    public List<BrandDTO> queryByBrandId(List<Long> ids) {


        List<BrandDTO> brandDTOS = BrandDTO.convertEntityList(this.listByIds(ids));


        return brandDTOS;
    }

    @Override
    public List<BrandDTO> queryByCategoryId(Long id) {


        List<BrandDTO> category_id = BrandDTO.convertEntityList(this.listByIds(this.categoryBrandService
                .query()
                .eq("category_id", id)
                .list()
                .stream()
                .map(CategoryBrand::getBrandId)
                .collect(Collectors.toList())));
        return category_id;


    }

    @Override
    public BrandDTO queryBrandByBrandId(Long id) {
        return new BrandDTO(this.getById(id));
    }

    @Override
    public PageDTO<BrandDTO> pageQuery(Integer page, Integer rows,String key) {
        IPage<Brand> brandPage = new Page<>(page, rows);

        //select * from tb_brand [where name like %key% or letter = key] limit 0,5
        //QueryWrapper是查询条件的封装，并不是整个sql的条件，分页条件要和这个条件融合
        //这里只是条件的定义，真正的执行，会在内部二次封装
        page(brandPage,new QueryWrapper<Brand>()
                        .like(StringUtils.isNotBlank(key),"name",key)
                        .or()
                        .eq(StringUtils.isNotBlank(key),"letter",key));//分页查询不用获取相当于信息发生了自动回填，类似于主键回显

        return new PageDTO<>(brandPage.getTotal(),brandPage.getPages(),BrandDTO.convertEntityList(brandPage.getRecords()));
    }

    @Override
    public void addBrand(BrandDTO brandDTO) {
        //dto  ==> entity
        Brand brand = brandDTO.toEntity(Brand.class);

        //保存品牌并回显id
        this.save(brand);

        //从dto中获取分类的ID集合，并结合品牌id，生成中间表对象
        List<CategoryBrand> categoryBrands = brandDTO
                        .getCategoryIds()
                        .stream()
                        .map(cid -> CategoryBrand.of(cid, brand.getId()))
                        .collect(Collectors.toList());

        this.categoryBrandService.saveBatch(categoryBrands);

    }

    @Override
    public void upDateBrandById(BrandDTO brandDTO) {
        //类型转换并更新
        Brand brand = brandDTO.toEntity(Brand.class);
        this.updateById(brand);

        //根据brandid 删除中间表
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",brand.getId()));


        //重新创建中间表
        List<CategoryBrand> categoryBrands = brandDTO.getCategoryIds()
                .stream()
                .map(cid -> CategoryBrand.of(cid, brand.getId()))
                .collect(Collectors.toList());
        this.categoryBrandService.saveBatch(categoryBrands);


    }

    @Override
    public void deleteBrandById(Long id) {



        //1.根据brandid把中间表的关系删除
        this.categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",id));
        //2.删除id对应的品牌
        this.removeById(id);
    }


}
